//
//  RequestNetWorkTool.swift
//  LivelyChat
//
//  Created by admin on 2021/6/23.
//

import UIKit
let RT = RequestNetWorkTool.shared

class RequestNetWorkTool: NSObject {
    static let shared = RequestNetWorkTool()
    
    func GuestLogininWith(emailNum imei:String, Handler comp:@escaping ((InfoUtil.initInfo) -> Void)) {
        var para:[String : Any] = [:]
        para["identifier"] = imei
        RequestManger.postRequest(UrlString: guestLoginUrl, params: para, success: { (response) in
            print("getUserInfoUrlInfoWith=-----   ")
            print(response)
            let jsonData = InfoUtil.initInfo.deserialize(from: response)
            USEINFOMODEL = jsonData!
            let userdefalut = UserDefaults.standard
            userdefalut.setValue(jsonData?.toJSON(), forKey: "UserInfo")
            comp(jsonData!)
        }) { (error) in
            print(error)
        }
    }

    // 登录
    func appleLogininWith(emailNum email:String, Handler comp:@escaping ((InfoUtil.initInfo) -> Void)) {
        var para:[String : Any] = [:]
        para["email"] = email
        RequestManger.postRequest(UrlString: appleLoginUrl, params: para, success: { (response) in
            
            print("getUserInfoUrlInfoWith=-----   ")
            print(response)

            let jsonData = InfoUtil.initInfo.deserialize(from: response)
            USEINFOMODEL = jsonData!
            let userdefalut = UserDefaults.standard
            userdefalut.setValue(jsonData?.toJSON(), forKey: "UserInfo")
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }
    
    func buySuccessWith(code codeStr:String,coins:String, Handler comp:@escaping ((InfoUtil.initInfo) -> Void)) {
        var para:[String : Any] = [:]
        para["userId"] = USEINFOMODEL.id
        para["code"] = codeStr
        para["coins"] = coins

        RequestManger.postRequest(UrlString: rechargeUrl, params: para, success: { (response) in
            
            print("getUserInfoUrlInfoWith=-----   ")
            print(response)

            let jsonData = InfoUtil.initInfo.deserialize(from: response)
            USEINFOMODEL = jsonData!
            
            let userdefalut = UserDefaults.standard
            userdefalut.setValue(jsonData?.toJSON(), forKey: "UserInfo")
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }

    
    // 登录
    func LogininWith(emailNum num:String, Password pwd:String, Handler comp:@escaping ((InfoUtil.initInfo) -> Void)) {
        var para:[String : Any] = [:]
        para["email"] = num
        para["pwd"] = pwd
        RequestManger.postRequest(UrlString: loginUrl, params: para, success: { (response) in
            
            print("getUserInfoUrlInfoWith=-----   ")
            print(response)

            let jsonData = InfoUtil.initInfo.deserialize(from: response)
            USEINFOMODEL = jsonData!
            let userdefalut = UserDefaults.standard
            userdefalut.setValue(jsonData?.toJSON(), forKey: "UserInfo")

            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }
    
    // 获取视频ticket
    func getUploadVideoTicket(userID:String, Handler comp:@escaping ((InfoUtil.videoTicket) -> Void)) {
        var para:[String:Any] = [:]
        para["userId"] = userID
        
        RequestManger.postRequest(UrlString: getVideoTicketUrl, params: para, success: { (response) in
            
            Toast_View_Tool.show(text: "Uploading")

            let jsonData = InfoUtil.videoTicket.deserialize(from: response)
            print(jsonData)
            print("jsonData=-=-----   ")
            
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }

        
    }

    
    func getVideoInfo(videoId:String, Handler comp:@escaping ((InfoUtil.videoInfoList) -> Void)) {
        var para:[String:Any] = [:]
        para["videoId"] = videoId
        
        RequestManger.postRequest(UrlString: getMajiaPlayInfoUrl, params: para, success: { (response) in
            
            let jsonData = InfoUtil.videoInfoList.deserialize(from: response)
            
            print(jsonData)
            print("jsonData=-=-----   ")
            
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }

        
    }

    func uploadAudioFile(file:Data, Handler comp:@escaping ((InfoUtil.uplpadImg) -> Void)) {
        var para:[String:Any] = [:]
        para["file"] = file
        
        RequestManger.uploadAudioRequest(UrlString: uploadImageUrl, params: para) { (response) in
            let imgDict = response as NSDictionary
            
            print("uploadAudioFile==----   ")
            print(imgDict)
            
            comp(InfoUtil.uplpadImg.deserialize(from: imgDict)!)
            
        } failure: { (error) in
            
        }
        
    }

    func uploadCoverImage(file:Data, Handler comp:@escaping ((InfoUtil.uplpadImg) -> Void)) {
        var para:[String:Any] = [:]
        para["file"] = file
        
        RequestManger.uploadImgRequest(UrlString: uploadImageUrl, params: para) { (response) in
            let imgDict = response as NSDictionary
            comp(InfoUtil.uplpadImg.deserialize(from: imgDict)!)
        } failure: { (error) in
            
        }
        
    }
   
    func createHouse(paraInfo paraInfoDict:[String:Any], Handler comp:@escaping ((InfoUtil.room) -> Void)) {
        RequestManger.postRequest(UrlString: createHouseurl, params: paraInfoDict, success: { (response) in
            let jsonData = InfoUtil.room.deserialize(from: response)
            print("creteHouse===-----   ")
            print(jsonData)
            comp(jsonData!)
        }) { (error) in
            print(error)
        }
    }
    
    func updateHouse(paraInfo paraInfoDict:[String:Any], Handler comp:@escaping ((InfoUtil.room) -> Void)) {
        RequestManger.postRequest(UrlString: updateHouseurl, params: paraInfoDict, success: { (response) in
            
            let jsonData = InfoUtil.room.deserialize(from: response)
            
            print("updateHouse===-----   ")
            print(jsonData)
            
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }

    
    func getRoomList(Handler comp:@escaping ((InfoUtil.roomList) -> Void)) {
        
        var para:[String : Any] = [:]
        para["userId"] = USEINFOMODEL.id
        RequestManger.postRequest(UrlString: houseList, params: para, success: { (response) in
            print("GetRoomListWith=-----   ")
            print(response)
            let jsonData = InfoUtil.roomList.deserialize(from: response)
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
        
    }

    func deletUserWith(Handler comp:@escaping ((String) -> Void)) {
        var para:[String : Any] = [:]
        para["userId"] = USEINFOMODEL.id
        RequestManger.postRequest(UrlString: delAccountUrl, params: para, success: { (response) in
            comp("1")
        }) { (error) in
            print(error)
        }
    }
    
    func blockWithUserid(userid uid:String,type:String,Handler comp:@escaping ((String) -> Void)) {
        var para:[String : Any] = [:]
        para["otherUserId"] = uid
        para["type"] = type
        para["userId"] = USEINFOMODEL.id
        RequestManger.postRequest(UrlString: blockAnchor, params: para, success: { (response) in
            comp("1")
        }) { (error) in
            print(error)
        }
    }
    
    func registerWith(emailNum num:String, Password pwd:String, Handler comp:@escaping ((String) -> Void)) {
        var para:[String : Any] = [:]
        para["email"] = num
        para["pwd"] = pwd
        RequestManger.postRequest(UrlString: regUrl, params: para, success: { (response) in
            
            let jsonData = InfoUtil.register.deserialize(from: response)
            USEINFOMODEL.id = jsonData?.userId ?? ""
            comp(jsonData?.userId ?? "")
            
        }) { (error) in
            print(error)
        }
    }
    
    
    func searchanListWith(page pagestr:String,random randomStr:String, Handler comp:@escaping ((InfoUtil.anchorList) -> Void)) {
        var para:[String : Any] = [:]
        para["page"] = "0"
        para["random"] = "0"
        para["size"] = "100"
        
        RequestManger.postRequest(UrlString: userListUrl, params: para, success: { (response) in
            let jsonData = InfoUtil.anchorList.deserialize(from: response)
            
            print(jsonData)
            print("searchanList=------   ")
            
            comp(jsonData!)
        }) { (error) in
            print(error)
        }
    }

    func blockListWith(Handler comp:@escaping ((InfoUtil.anchorList) -> Void)) {
        var para:[String : Any] = [:]
        para["userId"] = USEINFOMODEL.id
        RequestManger.postRequest(UrlString: blockUserList, params: para, success: { (response) in
            let jsonData = InfoUtil.anchorList.deserialize(from: response)
            comp(jsonData!)
        }) { (error) in
            print(error)
        }
    }
    
    func anchorListWith(page pagestr:String,random randomStr:String, Handler comp:@escaping ((InfoUtil.anchorList) -> Void)) {
        var para:[String : Any] = [:]
        para["page"] = pagestr
        para["random"] = randomStr
        
        RequestManger.postRequest(UrlString: userListUrl, params: para, success: { (response) in
            let jsonData = InfoUtil.anchorList.deserialize(from: response)
            
            print(jsonData)
            print("anchorListWith=------   ")
            print(response)

            comp(jsonData!)
        }) { (error) in
            print(error)
        }
    }

        
    func anchorInfoWith(anchorId anchorIdtr:String, Handler comp:@escaping ((InfoUtil.anchorInfo) -> Void)) {
        var para:[String : Any] = [:]
        para["anchorId"] = anchorIdtr
        
        RequestManger.postRequest(UrlString: anchorinfoUrl, params: para, success: { (response) in
            let jsonData = InfoUtil.anchorInfo.deserialize(from: response)
            

            print(jsonData)
            print("anchorListWith=------   ")
            
            comp(jsonData!)
        }) { (error) in
            print(error)
        }
    }
    
    func buyGiftWith(cost:String, Handler comp:@escaping ((InfoUtil.initInfo) -> Void)) {
        var para:[String : Any] = [:]
        para["userId"] = USEINFOMODEL.id
        para["cost"] = cost
//  
        RequestManger.postRequest(UrlString: buyUrl, params: para, success: { (response) in
            let jsonData = InfoUtil.initInfo.deserialize(from: response)
            
            print("getUserInfoUrlInfoWith=-----   ")
            print(response)
            
            USEINFOMODEL = jsonData!
            let userdefalut = UserDefaults.standard
            userdefalut.setValue(jsonData?.toJSON(), forKey: "UserInfo")
            
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }

    
    func initUserInfoWith(paraInfo paraInfoDict:[String:Any], Handler comp:@escaping ((InfoUtil.initInfo) -> Void)) {
        
        RequestManger.postRequest(UrlString: reg_infoUrl, params: paraInfoDict, success: { (response) in
            let jsonData = InfoUtil.initInfo.deserialize(from: response)
            print("getUserInfoUrlInfoWith=-----   ")
            print(response)
            USEINFOMODEL = jsonData!
            let userdefalut = UserDefaults.standard
            userdefalut.setValue(jsonData?.toJSON(), forKey: "UserInfo")
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }

    
    func getOtherUserInfoUrlInfoWith(userid useridStr:String, Handler comp:@escaping ((InfoUtil.initInfo) -> Void)) {
        
        var para:[String : Any] = [:]
        para["userId"] = useridStr
        RequestManger.postRequest(UrlString: userInfoUrl, params: para, success: { (response) in
            let jsonData = InfoUtil.initInfo.deserialize(from: response)
            print("getOtherUserInfoUrlInfoWith=-----   ")
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }
     
    func favHouseListAction(Handler comp:@escaping ((InfoUtil.roomList) -> Void)) {
        var para:[String : Any] = [:]
        para["userId"] = USEINFOMODEL.id
        RequestManger.postRequest(UrlString: favHouseListUrl, params: para, success: { (response) in
            let jsonData = InfoUtil.roomList.deserialize(from: response)
            print("updateHouse===-----   ")
            comp(jsonData!)
        }) { (error) in
            print(error)
        }
    }

    // 类型 0取消收藏 1收藏
    func collectRoom(idstring:String,type:String, Handler comp:@escaping ((String) -> Void)) {
        var para:[String : Any] = [:]
        para["houseId"] = idstring
        para["userId"] = USEINFOMODEL.id
        para["type"] = type
        RequestManger.postRequest(UrlString: favHouseUrl, params: para, success: { (response) in
            comp("1")
        }) { (error) in
            print(error)
        }
    }

    // 0取消关注 1关注
    func AddDeleteFriend(idstring:String,type:String, Handler comp:@escaping ((String) -> Void)) {
        var para:[String : Any] = [:]
        para["otherUserId"] = idstring
        para["userId"] = USEINFOMODEL.id
        para["type"] = type
        RequestManger.postRequest(UrlString: addFriendUrl, params: para, success: { (response) in
            comp("1")
        }) { (error) in
            print(error)
        }
    }
    func careFriendState(idstring:String, Handler comp:@escaping ((String) -> Void)) {
        var para:[String : Any] = [:]
        para["otherUserId"] = idstring
        para["userId"] = USEINFOMODEL.id
        RequestManger.postRequest(UrlString: careStateUrl, params: para, success: { (response) in
            let jsonData = InfoUtil.careState.deserialize(from: response)
            comp(jsonData?.state ?? "0")
        }) { (error) in
            print(error)
        }
    }
    
    func FriendListAction(Handler comp:@escaping (([InfoUtil.initInfo?]) -> Void)) {
        var para:[String : Any] = [:]
        para["userId"] = USEINFOMODEL.id
        RequestManger.postRequest(UrlString: friendListUrl, params: para, success: { (response) in
            let jsonData = [InfoUtil.initInfo].deserialize(from: response)
            comp(jsonData!)
        }) { (error) in
            print(error)
        }
    }
    
    func fansListAction(Handler comp:@escaping (([InfoUtil.initInfo?]) -> Void)) {
        var para:[String : Any] = [:]
        para["userId"] = USEINFOMODEL.id
        RequestManger.postRequest(UrlString: fansListUrl, params: para, success: { (response) in
            let jsonData = [InfoUtil.initInfo].deserialize(from: response)
            comp(jsonData!)
        }) { (error) in
            print(error)
        }
    }

    func fansNumAction(idstring:String,Handler comp:@escaping ((InfoUtil.numModel) -> Void)) {
        var para:[String : Any] = [:]
        para["userId"] = idstring
        RequestManger.postRequest(UrlString: fansNumUrl, params: para, success: { (response) in
            let jsonData = InfoUtil.numModel.deserialize(from: response)
            comp(jsonData!)
        }) { (error) in
            print(error)
        }
    }
    
    func careNumAct(idstring:String,Handler comp:@escaping ((InfoUtil.numModel) -> Void)) {
        var para:[String : Any] = [:]
        para["userId"] = idstring
        RequestManger.postRequest(UrlString: careNumUrl, params: para, success: { (response) in
            let jsonData = InfoUtil.numModel.deserialize(from: response)
            comp(jsonData!)
        }) { (error) in
            print(error)
        }
    }



    func deleteRoom(idstring:String, Handler comp:@escaping ((String) -> Void)) {
        
        var para:[String : Any] = [:]
        para["id"] = idstring
        RequestManger.postRequest(UrlString: delHouse, params: para, success: { (response) in
            comp("1")
        }) { (error) in
            print(error)
        }
    }


    
    func getUserInfoUrlInfoWith(Handler comp:@escaping ((InfoUtil.initInfo) -> Void)) {
        
        var para:[String : Any] = [:]
        para["userId"] = USEINFOMODEL.id

        RequestManger.postRequest(UrlString: userInfoUrl, params: para, success: { (response) in
            
            print("getUserInfoUrlInfoWith=-----   ")
            print(response)

            let jsonData = InfoUtil.initInfo.deserialize(from: response)
            USEINFOMODEL = jsonData!
            let userdefalut = UserDefaults.standard
            userdefalut.setValue(jsonData?.toJSON(), forKey: "UserInfo")
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }

    
    
    func EditUserInfoWith(paraInfo paraInfoDict:[String:Any], Handler comp:@escaping ((InfoUtil.initInfo) -> Void)) {
        
        RequestManger.postRequest(UrlString: editInfoUrl, params: paraInfoDict, success: { (response) in
            
            print("getUserInfoUrlInfoWith=-----   ")
            print(response)

            let jsonData = InfoUtil.initInfo.deserialize(from: response)
            USEINFOMODEL = jsonData!
            let userdefalut = UserDefaults.standard
            userdefalut.setValue(jsonData?.toJSON(), forKey: "UserInfo")
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }


    
}
